#ifndef SOUNDIN_H
#define SOUNDIN_H

#include <QtCore>
#include <QAudioDeviceInfo>
#include <QAudioInput>
#include <QtNetwork/QUdpSocket>
#include <valarray>

#ifdef Q_OS_WIN32
#include <winsock.h>
#else
#include <sys/socket.h>
#endif //Q_OS_WIN32

// Thread gets audio data from soundcard and signals when a buffer of
// specified size is available.
class SoundInThread : public QThread
{
  Q_OBJECT
  bool quitExecution;           // if true, thread exits gracefully
  QMutex quitExecutionMutex;    // protects the quitExecution variable
  QAudioDeviceInfo inputDevice; // audioinput device name
  double m_rate;                // sample rate
  unsigned bufSize;             // user's buffer size

protected:
  virtual void run();

public:
  SoundInThread():
    quitExecution(false),
    m_rate(0),
    bufSize(262144)
//  bufSize(4096)
  {
  }

  // These have no effect if thread is already running:
  void setInputDevice(const QAudioDeviceInfo& inputDevice);
  void setRate(double rate);
  void setBufSize(unsigned bufSize);
  void setNetwork(bool b);

signals:
  void bufferAvailable(std::valarray<qint16> samples, double rate);
  void readyForFFT(int kb, int k);
  void error(const QString& message);
  void status(const QString& message);

public slots:
  void quit();

private:
  void inputUDP();

  bool m_net;
  QUdpSocket *udpSocket;
};

extern "C" {
  void recvpkt_(quint16* iblk, qint8* nrx, int* kb, int* k, double s1[],
                double s2[]);
}

#endif // SOUNDIN_H
